import { type PaginationProps } from "@pureadmin/table";
import { GetIndexModelRateList, GetIndexModexRateBaseData, AddIndexModelRate, GetScoreResult } from "@/api/stu/bankBigData";
import { useRoute } from "vue-router";
import { ElMessage, FormInstance } from "element-plus";
import { ref, reactive, onMounted } from "vue";
export function useCommon() {
  const route = useRoute();
  const customerVisible = ref<boolean>(false);
  const resultVisible = ref<boolean>(false);
  const resultList = ref()
  const selectData = ref()
  const dataList = ref<any>([]);
  const showId = ref()
  const loading = ref<boolean>(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    background: true,
    pageSizes: [5, 10, 30],
  });
  const pagination1 = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
    pageSizes: [5, 10, 15],
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      width: 120
    },
    {
      label: "接入数据名称",
      prop: "dataName",
      minWidth: 200
    },
    {
      label: "指数体系名称  ",
      prop: "indexSystemName",
      minWidth: 300
    },
    {
      label: "详情",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];

  const resultColumn: TableColumnList = [
    {
      label: "序号",
      type: "index",
      width: 50
    },
    {
      label: "姓名",
      prop: "name",
    },
    {
      label: "年龄",
      prop: "age",
      width: 50,
    },
    {
      label: "姓别",
      prop: "gender",
      width: 50,
    },
    {
      label: "指数模型名称",
      prop: "indexModelName",
    },
    {
      label: "消费能力指数",
      prop: "spendingPowerScore",
    },
    {
      label: "网购倾向指数",
      prop: "onlineShoppingScore",
    },
    {
      label: "社交活跃性指数",
      prop: "socialActivityScore",
    },

    {
      label: "资金需求指数",
      prop: "fundingRequirementsScore",
    },
    {
      label: "恶意透支指数",
      prop: "maliciousOverdraftScore",
    },
    {
      label: "履约能力指数",
      prop: "performanceCapabilityScore",
    },
    {
      label: "游戏沉迷指数",
      prop: "gameAddictionScore",
    },
    {
      label: "个人稳定性指数",
      prop: "personalStabilityScore",
    }
  ];

  const customerRatingForm = reactive({
    apiInfoDataShareId: '',
    portraitIndexSystemId: '',
    scoringRules: '',
  })

  const rule = {
    apiInfoDataShareId: [
      {
        required: true,
        message: "请选择接入数据名称",
        trigger: ["blur", "change"]
      }
    ],
    portraitIndexSystemId: [
      {
        required: true,
        message: "请选择指数体系名称",
        trigger: ["blur", "change"]
      }
    ],
    scoringRules: [
      {
        required: true,
        message: "请选择指数评分规则",
        trigger: ["blur", "change"]
      }
    ],
  }
  const BtnLoading = ref<boolean>(false)
  function OUTBtnLoding() {
    setTimeout(() => {
      BtnLoading.value = false
    }, 1000)
  }

  //获取列表数据
  async function getList() {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
    };
    const respone = await GetIndexModelRateList(data);
    if (respone.success) {
      respone.data.items.forEach((item) => {
        item.createTime = item.createTime.substring(0, 10)
      })
      dataList.value = respone?.data.items
      pagination.total = respone.data.totalCount;
    } else {
      ElMessage.error(respone.msg);
    }
  }


  function cancelCustomerRating() {
    for (var prop in customerRatingForm) {
      if (customerRatingForm.hasOwnProperty(prop)) {
        customerRatingForm[prop] = ''
      }
    }
    customerVisible.value = false
  }

  function handleSizeChange(val) {
    getList()
  }

  function handleCurrentChange(val) {
    getList()
  }

  onMounted(async () => {
    // 获取新增的基本信息
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
    }
    let res = await GetIndexModexRateBaseData(data)
    if (res.success) {
      selectData.value = res?.data
    } else {
      ElMessage.error(res.msg);
    }
    getList()
  })

  const saveCustomerRating = (customerForm: FormInstance | undefined) => {
    (customerForm as any).validate(async valid => {
      if (!valid) return;
      const data = {
        taskId: route.query.taskId,
        planId: route.query.planId,
        apiInfoDataShareId: customerRatingForm.apiInfoDataShareId,
        portraitIndexSystemId: customerRatingForm.portraitIndexSystemId,
        scoringRules: customerRatingForm.scoringRules,
        studentAnswer: JSON.stringify(customerRatingForm)
      };
      BtnLoading.value = true;
      OUTBtnLoding()
      const respone = await AddIndexModelRate(data);
      if (respone.data) {
        ElMessage.success(respone.msg)
        cancelCustomerRating()
        getList()
      } else {
        ElMessage.error(respone.msg);
      }
    })
  }

  async function CheckCreditReport(row) {
    if (row) {
      showId.value = row.indexModelRatingRuleId
    }
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      detailId: showId.value,
      pageIndex: pagination1.currentPage,
      pageCount: pagination1.pageSize,
    };
    const respone = await GetScoreResult(data);
    if (respone.success) {
      resultList.value = respone.data.items
      pagination1.total = respone.data.totalCount;
      resultVisible.value = true
    } else {
      ElMessage.error(respone.msg);
    }
  }


  function handleSizeChange1(val) {
    CheckCreditReport()
  }

  function handleCurrentChange1(val) {
    CheckCreditReport()
  }

  return {
    dataList,
    loading,
    pagination,
    pagination1,
    columns,
    resultColumn,
    rule,
    BtnLoading,
    selectData,
    customerVisible,
    customerRatingForm,
    resultVisible,
    resultList,
    CheckCreditReport,
    saveCustomerRating,
    cancelCustomerRating,
    handleCurrentChange,
    handleSizeChange,
    handleCurrentChange1,
    handleSizeChange1,
  }
}
